﻿namespace QMVnSoft.DAL
{
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;
    using QMVnSoft.DAL.Util;    

    public class UserDataAccess
    {        
        public int ChangePassword(long pi_Stt, string ps_Password)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", pi_Stt), new SqlParameter("p_User_PWD", AppSecurity.Base64Encode(ps_Password.Equals(string.Empty) ? "EZSoft" : ps_Password)), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[2].Direction = ParameterDirection.Output;
            values[2].SqlDbType = SqlDbType.Int;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "User_PWD_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
                num = (int) values[2].Value;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }

        public DataSet GetDataSet()
        {
            DataSet set2;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            DataSet dataSet = new DataSet();
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "UserList_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                adapter.SelectCommand = command;
                adapter.Fill(dataSet);
                AppDataAccess.CloseConnection(eZConnection);
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return set2;
        }

        public DataTable GetTable(string ps_UserName)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_UserName", ps_UserName) };
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            DataTable dataTable = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "UserList_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                AppDataAccess.CloseConnection(eZConnection);
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return table2;
        }

        public string GetUserName(string ps_UserName)
        {
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_FullName", null) };
            values[1].Direction = ParameterDirection.Output;
            values[1].SqlDbType = SqlDbType.NVarChar;
            values[1].Size = 0x80;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "UserName_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            return (string) values[1].Value;
        }

        public DataTable GetUserRights(string ps_FuncType, object pi_Usr_GrpID, string ps_Usr_Grp)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_FuncType", ps_FuncType), new SqlParameter("p_Usr_GrpID", pi_Usr_GrpID), new SqlParameter("p_Usr_Grp", ps_Usr_Grp) };
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            DataTable dataTable = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "UserRights_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                AppDataAccess.CloseConnection(eZConnection);
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return table2;
        }

        public int GetUserType(string ps_UserName)
        {
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_UserType", null) };
            values[1].Direction = ParameterDirection.Output;
            values[1].SqlDbType = SqlDbType.Int;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "UserType_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            return (int) values[1].Value;
        }

        public void ResetRights(string ps_UserOrGroup, object pi_UserOrder)
        {
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_UserID", pi_UserOrder), new SqlParameter("p_Usr_Grp", ps_UserOrGroup) };
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "UserRights_Delete";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
        }

        public void SaveRights(string ps_UserOrGroup, object pi_UserOrder, string ps_FuncType, DataRow row)
        {
            SqlParameter[] values = new SqlParameter[10];
            values[0] = new SqlParameter("p_Usr_Grp", ps_UserOrGroup);
            values[1] = new SqlParameter("p_Usr_GrpID", pi_UserOrder);
            values[2] = new SqlParameter("p_FuncType", ps_FuncType);
            values[3] = new SqlParameter("p_FuncID", row["FuncID"]);
            values[4] = new SqlParameter("p_Right_1", row["Right_1"]);
            if (row.Table.Columns.Contains("Right_2"))
            {
                values[5] = new SqlParameter("p_Right_2", row["Right_2"]);
            }
            else
            {
                values[5] = new SqlParameter("p_Right_2", null);
            }
            if (row.Table.Columns.Contains("Right_3"))
            {
                values[6] = new SqlParameter("p_Right_3", row["Right_3"]);
            }
            else
            {
                values[6] = new SqlParameter("p_Right_3", null);
            }
            if (row.Table.Columns.Contains("Right_4"))
            {
                values[7] = new SqlParameter("p_Right_4", row["Right_4"]);
            }
            else
            {
                values[7] = new SqlParameter("p_Right_4", null);
            }
            if (row.Table.Columns.Contains("Right_5"))
            {
                values[8] = new SqlParameter("p_Right_5", row["Right_5"]);
            }
            else
            {
                values[8] = new SqlParameter("p_Right_5", null);
            }
            if (row.Table.Columns.Contains("Right_6"))
            {
                values[9] = new SqlParameter("p_Right_6", row["Right_6"]);
            }
            else
            {
                values[9] = new SqlParameter("p_Right_6", null);
            }
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "UserRights_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
        }

        public int SaveRow(bool pb_NewChecked, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Moi_Sua", pb_NewChecked), new SqlParameter("p_Stt", row["Stt"]), new SqlParameter("p_UserName", row["UserName"]), new SqlParameter("p_User_PWD", row["User_PWD"]), new SqlParameter("p_FullName", row["FullName"]), new SqlParameter("p_GrpCode", row["GrpCode"]), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[1].Direction = ParameterDirection.InputOutput;
            values[1].SqlDbType = SqlDbType.BigInt;
            values[6].Direction = ParameterDirection.Output;
            values[6].SqlDbType = SqlDbType.Int;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "UserList_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
                row["Stt"] = values[1].Value;
                num = (int) values[6].Value;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }

        public bool UserValidated(string ps_UserName, string ps_Password)
        {
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_User_PWD", AppSecurity.Base64Encode(ps_Password.Equals(string.Empty) ? "EZSoft" : ps_Password)), new SqlParameter("p_Success", null) };
            values[2].Direction = ParameterDirection.Output;
            values[2].SqlDbType = SqlDbType.Bit;
            values[2].Size = 1;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "User_Validated";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            return (bool) values[2].Value;
        }
    }
}

